package com.etalk.crm.controller.newmain;

import com.alibaba.fastjson.JSONObject;
import com.etalk.crm.pojo.NewMainBean;
import com.etalk.crm.service.AdminNewMainService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.etalk.crm.utils.PubResult.pubResultFunction;

/**
 * @author Jordan
 */
@RestController
@RequestMapping(value = "/adminNewMain",produces = "application/json; charset=utf-8")
public class AdminNewMainController {
    protected static final Logger logger = LogManager.getLogger(AdminNewMainController.class);

    @Resource
    private AdminNewMainService adminNewMainService;

    /**
     * 待约中教课学员
     * @param revisitId 客户属性id
     * @param sscId ssc id
     * @param page 页码
     * @param limit 页记录数
     * @return
     */
    @RequestMapping(value = "/not/booked/lt/class/list", method = RequestMethod.POST)
    @ResponseBody
    public String getNotBookedClassList(Integer sscId,Integer revisitId, Integer page, Integer limit) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        PageInfo<NewMainBean> pageInfo = adminNewMainService.searchNotBookedClassList(revisitId,sscId, page, limit);
        Map<String, Object> jsonMap = pubResultFunction(pageInfo);
        logger.info("首页admin待约中教课学员列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     * 未提交检测报告的学员名单
     * @param sscId
     * @param revisitId
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping(value = "/not/lt/feedback/list", method = RequestMethod.POST)
    @ResponseBody
    public String getLtNotFeedbackList(Integer sscId,Integer revisitId, Integer page, Integer limit) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        PageInfo<NewMainBean> pageInfo = adminNewMainService.searchLtNotFeedbackList(sscId, page, limit);
        Map<String, Object> jsonMap = pubResultFunction(pageInfo);
        logger.info("首页admin未提交检测报告列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    @RequestMapping(value = "/order/expire/list", method = RequestMethod.POST)
    @ResponseBody
    public String getStuOrderInfoList(Integer sscId,Integer ccId, Integer days, Integer page,Integer limit) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        PageInfo<NewMainBean> pageInfo = adminNewMainService.searchStudentOrderInfo(sscId, ccId,days,page,limit);
        Map<String, Object> jsonMap = pubResultFunction(pageInfo);
        logger.info("首页admin订单的60天或30到期信息或课时剩30以下列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     * 老学员跟进
     * @param sscId ssc id
     * @param ccId cc id
     * @param followDays 跟进天数
     * @param page 页码
     * @param limit 页记录数
     * @return
     */
    @RequestMapping(value = "/old/stu/follow/list", method = RequestMethod.POST)
    @ResponseBody
    public String getOcOldStudentFollowList(Integer sscId,Integer ccId,Integer followDays, Integer page,Integer limit) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        PageInfo<NewMainBean> pageInfo = adminNewMainService.searchOcOldStudentFollow(sscId, ccId,followDays,page,limit);
        Map<String, Object> jsonMap = pubResultFunction(pageInfo);
        logger.info("首页admin老学员跟进列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     * 潜在学员
     * @param state 客户属性
     * @param ccId cc id
     * @param page 页码
     * @param limit 页记录数
     * @return
     */
    @RequestMapping(value = "/potential/stu/list", method = RequestMethod.POST)
    @ResponseBody
    public String getPotentialStudentList(Integer state,Integer ccId,Integer page,Integer limit) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        PageInfo<NewMainBean> pageInfo = adminNewMainService.searchPotentialStudent(ccId,state,page,limit);
        Map<String, Object> jsonMap = pubResultFunction(pageInfo);
        logger.info("首页admin潜在学员列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     * 5天未约课学员
     * @param sscId ssc id
     * @param ccId cc id
     * @param page 页码
     * @param limit 页记录数
     * @return
     */
    @RequestMapping(value = "/inactive/lesson/list", method = RequestMethod.POST)
    @ResponseBody
    public String getInactiveLessonsList(Integer sscId,Integer ccId,Integer page,Integer limit) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        PageInfo<NewMainBean> pageInfo = adminNewMainService.searchInactiveLessons(sscId, ccId,page,limit);
        Map<String, Object> jsonMap = pubResultFunction(pageInfo);
        logger.info("首页admin潜在学员列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     * 学员连续旷课3次以上
     * @param sscId ssc id
     * @param ccId cc id
     * @param page 页码
     * @param limit 页记录数
     * @return
     */
    @RequestMapping(value = "/absenteeism/lesson/list", method = RequestMethod.POST)
    @ResponseBody
    public String getAbsenteeismLessonList(Integer sscId,Integer ccId,Integer page,Integer limit) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        PageInfo<NewMainBean> pageInfo = adminNewMainService.searchAbsenteeismLessonList(sscId, ccId,page,limit);
        Map<String, Object> jsonMap = pubResultFunction(pageInfo);
        logger.info("首页admin旷课学员列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     * 提醒列表
     * @param ccId cc id
     * @param searchDate 日期
     * @param page 页码
     * @param limit 页记录数
     * @return
     */
    @RequestMapping(value = "/track/remind/list", method = RequestMethod.POST)
    @ResponseBody
    public String getAbsenteeismLessonList(String ccId, String searchDate, Integer page, Integer limit) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }

        List<Integer> listCcId=new ArrayList<>();
        if (StringUtils.isNotEmpty(ccId) && !ccId.contains("null")){
            for (String id:ccId.split(",")){
                listCcId.add(Integer.parseInt(id));
            }
        }
        //PageInfo<NewMainBean> pageInfo = new PageInfo<>();
        PageInfo<NewMainBean> pageInfo = adminNewMainService.searchTrackRemindList(listCcId,searchDate,page,limit);
        Map<String, Object> jsonMap = pubResultFunction(pageInfo);
        logger.info("首页admin跟进提醒列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }
}
